/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rule;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author menandro.neto
 */
public class Calcula extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        int r = 0;
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String operacao = request.getParameter("operacao");
            String v1 = request.getParameter("txtValor1");
            String v2 = request.getParameter("txtValor2");
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Calcula</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Calcula at " + request.getContextPath () + "</h1>");
            
            if ( (v1 != null) && ( v2 != null ) && (operacao != null) ) { 
                if (operacao.equals("+") )
                   r = Integer.parseInt(v1) + Integer.parseInt(v2);
                else
                   if (operacao.equals("-") )
                      r = Integer.parseInt(v1) - Integer.parseInt(v2);
                   else
                      if (operacao.equals("x") )
                         r = Integer.parseInt(v1) * Integer.parseInt(v2);
                      else
                         if (operacao.equals("/") && Integer.parseInt(v2) != 0)
                            r = Integer.parseInt(v1) / Integer.parseInt(v2);
                         else
                            r = 0; 

                out.printf("%d %s %d = %d", Integer.parseInt(v1), operacao, Integer.parseInt(v2), r );
            }
            else 
                out.print("Parametros não definidos!");
            
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
